<template>
    <div class="addProject business-workAdd">
        <div class="addForm zll-form">
			<el-form :model="addForm" :rules="rules" ref="addForm" class="demo-ruleForm">
                <el-row :gutter="40">
                    <el-col :span="12">
                        <el-form-item class="formList" prop="fdType" label="违章类型：">
                            <el-select clearable class="input_right" v-model="addForm.fdType" :disabled="type == 'see' || type == 'approval'">
								<el-option label="安全纪律" value="0"></el-option>
								<el-option label="质量内控" value="1"></el-option>
								<el-option label="客户投诉" value="2"></el-option>
								<el-option label="仓储规范" value="3"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item class="formList" prop="fdDate" label="违章日期：">
                            <el-date-picker
								format="yyyy-MM-dd"
								value-format="yyyy-MM-dd"
								clearable class="input_right"  v-model="addForm.fdDate" :disabled="type == 'see' || type == 'approval'" type="date"></el-date-picker>
                        </el-form-item>
                        <el-form-item class="formList" prop="fdViolatorId" label="违章人：">
							<el-select filterable clearable v-model="addForm.fdViolatorId" placeholder="违章人" :disabled="type == 'see' || type == 'approval'">
								<el-option v-for="item in employees" :key="item.id" :label="item.employeeName" :value="item.id"></el-option>
							</el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item class="formList" prop="fdStatus" label="罚单状态：">
                            <el-select clearable class="input_right" v-model="addForm.fdStatus" disabled>
								<el-option label="待审批" value="0"></el-option>
								<el-option label="已审批" value="1"></el-option>
								<el-option label="已拒绝" value="2"></el-option>
								<el-option label="已作废" value="3"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item class="formList" prop="data5" label="罚单号：">
                            <el-input clearable class="input_right"  v-model="addForm.fdCode" disabled></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-form-item class="formList" prop="fdReason" label="违章事由：">
                    <el-input clearable class="input_right"  v-model="addForm.fdReason" :disabled="type == 'see' || type == 'approval'" :rows="2" maxlength="50" show-word-limit></el-input>
                </el-form-item>
                <el-row :gutter="40">
                    <el-col :span="12">
                        <el-form-item class="formList" prop="fdMoney" label="处罚金额(元)：">
                            <el-input clearable class="input_right"  v-model="addForm.fdMoney" :disabled="type == 'see' || type == 'approval'" ></el-input>
                        </el-form-item>
                        <el-form-item class="formList" prop="data8" label="开单人：">
                            <el-input clearable class="input_right"  v-model="addForm.createrName" disabled ></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item class="formList"></el-form-item>
                        <el-form-item class="formList" prop="data9" label="审批人：" v-if="type !== 'add'">
                            <el-input clearable class="input_right"  v-model="addForm.approvedName" disabled ></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </div>
    </div>
</template>
<script>
import {getStore} from "../../../../../plugins/localStore";
import {formatDate} from "../../../../../plugins/formatDate";
export default {
    props: ['editData','type','employeeList'],
    data(){
        return {
            addForm: {
            	id: '',
				fdType: '',
				fdCode:'',
				fdDate:'',
				fdStatus:'0',
				fdViolatorId: '',
				fdReason:'',
				fdMoney:'',
				createrName:'',
				approvedName:'',

            },

			employees: [],
			count:0,

			fdCreaterId:'',
			fdApproverId: '',

            rules: {
				fdType: [
                    {required: true, message: '请选择违章类型', trigger: 'change'},
                ],
				fdDate: [
                    {required: true, message: '请选择违章日期', trigger: 'change'},
                ],
				fdViolatorId: [
                    {required: true, message: '请输入违章人', trigger: 'blur'},
                ],
				fdStatus: [
                    {required: true, message: '请选择罚单状态', trigger: 'change'},
                ],
				fdReason: [
                    {required: true, message: '请输入违章事由', trigger: 'blur'},
                ],
				fdMoney: [
                    {required: true, message: '请输入处罚金额', trigger: 'blur'},
                ],
            }
        }
    },
    methods: {

		approveFd(val,status) {
			let self = this;
			let param = {
				id:self.addForm.id,
				fdStatus:status,
				fdApproverId:this.user.id,
				fdApproveTime:formatDate(new Date(), 'yyyy-MM-dd hh:mm:ss')
			};

			if (this.count == 0){
				self.count++;
				self.$http({
					url: "/jinl/violation/updateFdStatus",
					method: "post",
					data: JSON.stringify(param),
					dataType: 'json',
					contentType: 'application/json',
				}).then(resp=>{
					if (resp.success) {
						self.$message.success('操作成功');
						self.$emit('refreshData');
						return;
					}
					self.$message.error(resp.message);
					self.count--;

				});
			}



		},

		submitForm(val,type) {
			let self = this;

            self.$refs['addForm'].validate((valid) => {
                if (valid) {
					self.operate();
                } else {
                    return false;
                }
            });
		},

		operate() {
			let self = this;
			let url = self.addForm.id ? '/jinl/violation/updateViolation' : '/jinl/violation/addViolation';

			let param = {
				id: self.addForm.id,
				fdCode: self.addForm.fdCode,
				fdType: self.addForm.fdType,
				fdReason: self.addForm.fdReason,
				fdStatus: '0',
				fdDate: self.addForm.fdDate,
				fdMoney: self.addForm.fdMoney,
				fdViolatorId: self.addForm.fdViolatorId,
				fdCreaterId : this.user.id
			}

			if (this.count == 0){
				self.count++;
				self.$http({
					url: url,
					method: "post",
					data: JSON.stringify(param),
					dataType: 'json',
					contentType: 'application/json',
				}).then(resp=> {
					if (resp.success) {
						self.$message.success('操作成功');
						self.$emit('refreshData');
						return;
					}
					self.$message.error(resp.message);
					self.count--;
				})
			}

		}
    },
    mounted(){
		this.user = getStore();
    },

	watch: {
		editData(val) {
			this.employees = JSON.parse(JSON.stringify(this.employeeList || []));
			this.fdCreaterId = '';
			this.fdApproverId = '';

			if (Object.getOwnPropertyNames(val).length > 1){
				this.addForm.id = val.id;
				this.addForm.fdType = val.fdType;
				this.addForm.fdCode = val.fdCode;
				this.addForm.fdDate = val.fdDate;
				this.addForm.fdStatus = val.fdStatus;
				this.addForm.fdViolatorId = val.fdViolatorId;
				this.addForm.fdReason = val.fdReason;
				this.addForm.fdMoney = val.fdMoney;
				this.addForm.createrName = val.createrName;
				this.addForm.approvedName = val.approvedName;

				this.fdCreaterId = val.fdCreaterId;
				this.fdApproverId = val.fdApproverId;
			}

		}
	}
}
</script>
<style lang="scss" scoped>
@import "@/style/dialog.scss";
.formList {
    height: 32px;
}
</style>